package com.umeox.phone.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.widget.SeekBar;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.widget.VerticalSeekBar;
import com.umeox.phone.R;
import com.umeox.phone.base.BaseApplication;
import com.umeox.watch.moto.dataservice.constants.Constants;


/**
 * Created by zl on 2017/9/15.
 */

public class VolumeActivity extends ThemeAppCompatActivity {

    private static final String TAG = "VolumeActivity";
    public static final String KEY_VOLUME_RESULT = "volume";
    private VerticalSeekBar mSeekBar;

    AudioManager mAudioManager;
    int currentVolume;
    int maxVolume;
    private int mTouchLimit;


    @SuppressLint("ClickableViewAccessibility")
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volume_activity);

        BaseApplication.getInstance().addActivity(this);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.setMode(AudioManager.MODE_IN_CALL);
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);

        mSeekBar = findViewById(R.id.sl_volume_call);
        mSeekBar.setMax(maxVolume);
        mSeekBar.setProgress(currentVolume);
        mTouchLimit = 1;

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                currentVolume = progress;
                mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, progress, AudioManager.STREAM_VOICE_CALL);
                mAudioManager.setMode(AudioManager.MODE_IN_CALL);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        mSeekBar.setOnTouchListener((v, event) -> {
            int height = mSeekBar.getHeight();
            if (mTouchLimit > 0) {
                float limitHeight = (float) (height / maxVolume) * mTouchLimit;
                Logger.e("limitHeight=" + limitHeight + ",event.getY() =" + event.getY());
                if (event.getY() > height - limitHeight) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        final long downTime = SystemClock.uptimeMillis();
                        MotionEvent motionEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_UP, event.getX(), height-limitHeight, 0);
                        mSeekBar.onTouchEvent(motionEvent);
                    }
                    return true;
                }
            }
            return false;
        });


    }

    @Override
    protected void onPause() {
        final Intent intent = new Intent(Constants.ACTION_VOLUME_VOICE_CALL);
        intent.putExtra(Constants.ACTION_VOLUME_VOICE_CALL, currentVolume);
        sendBroadcast(intent);
        super.onPause();

    }


    @Override
    public void onBackPressed() {

        Intent data = new Intent();
        data.putExtra(KEY_VOLUME_RESULT, currentVolume);
        setResult(RESULT_OK, data);
        finish();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VolumeActivity.class);
        context.startActivity(intent);
    }

}
